/*
 * ECARX Technology Limited is the owner of the copyright and the trade secret of this software.
 * Without permission, no one has the right to obtain, disclose or use this software in any way.
 */

package com.ecarx.custom.view.time;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

public class CustomDateTimeView extends FrameLayout implements UpdateFormatDateListener {

    private static final String TAG = "CustomDateTimeViewTag";

    private CustomTimeView customTimeView;
    private TextView tvDate;

    public CustomDateTimeView(Context context) {
        this(context, null);
    }

    public CustomDateTimeView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomDateTimeView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        View root = LayoutInflater.from(getContext()).inflate(R.layout.layout_time_date_view, this);
        customTimeView = root.findViewById(R.id.show_time_view);
        customTimeView.setUpdateFormatDateListener(this);
        tvDate = root.findViewById(R.id.tv_date);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
    }

    public void localTime(){
        if(customTimeView!=null){
            customTimeView.setLocaleDateFormat();
        }
    }

    public void setLabelMargin(int margin){
        if(customTimeView!=null){
            customTimeView.setLabelMargin(margin);
        }
    }

    public void setVerMargin(int topMara){
        if(tvDate!=null){
            LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) tvDate.getLayoutParams();
            layoutParams.topMargin = topMara;
            tvDate.setLayoutParams(layoutParams);
        }
    }

    public void setDateTextSize(int size) {
        if(tvDate!=null){
            tvDate.setTextSize(getResources().getDimensionPixelSize(size));
        }
    }

    public void setDateTextColor(int color) {
        if(tvDate!=null){
            tvDate.setTextColor(getResources().getColor(color));
        }
    }

    public void setTimeTextSize(int allSize) {
        if(customTimeView!=null){
            customTimeView.setAllTextSize(allSize);
        }
    }

    public void setTimeTextSize(int leftSize,int timeSize,int rightSize) {
        if(customTimeView!=null){
            customTimeView.setLeftSize(leftSize);
            customTimeView.setTimeSize(timeSize);
            customTimeView.setRightSize(rightSize);
        }
    }

    public void setTimeTextColor(int allColor) {
        if(customTimeView!=null){
            customTimeView.setAllTextColorRes(allColor);
        }
    }

    public void setTimeTextColor(int leftColor,int centerColor,int rightColor) {
        if(customTimeView!=null){
            customTimeView.setLeftColorRes(leftColor);
            customTimeView.setCenterColorRes(centerColor);
            customTimeView.setRightColorRes(rightColor);
        }
    }

    @Override
    public void formatDate(String language, int month, int day, int week) {
        Log.d(TAG, "formatDate - language:" + language + "--month:" + month + "--day:" + day + "--week:" + week);
        String[] months = getResources().getStringArray(R.array.month);
        String[] weeks = getResources().getStringArray(R.array.week);
        String[] days = getResources().getStringArray(R.array.day);
        StringBuilder stringBuilder = new StringBuilder();
        if (isCn(language)) {
            stringBuilder.append(months[month]);
            stringBuilder.append(days[day-1]);
            stringBuilder.append("  ");
            stringBuilder.append(weeks[week-1]);
        } else {
            stringBuilder.append(weeks[week-1]);
            stringBuilder.append(", ");
            stringBuilder.append(days[day-1]);
            stringBuilder.append(" ");
            stringBuilder.append(months[month]);
        }
        String strDate = stringBuilder.toString();
        Log.d(TAG, "formatDate - strDate = " + strDate);
        if (tvDate != null) {
            tvDate.setText(strDate);
        }
    }

    private boolean isCn(String language) {
        return CustomTimeView.LANGUAGE_CN.equalsIgnoreCase(language);
    }
}
